package com.example.KeHuDuan;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.client.SocketClient;
import com.example.scan2.Constant;
import com.example.scan2.Scan;
import com.example.scan2.WifiConnect;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    ImageButton musicBtn;
    ImageButton twoBarCodeBtn;
    ImageButton videoBtn;
    @Override
    protected  void sendHandlerToService(){

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        musicBtn = (ImageButton) findViewById(R.id.musicBtn);
        videoBtn = (ImageButton) findViewById(R.id.videoBtn);
        twoBarCodeBtn = (ImageButton) findViewById(R.id.twoBarCodeBtn);

        musicBtn.setOnClickListener(this);
        videoBtn.setOnClickListener(this);
        twoBarCodeBtn.setOnClickListener(this);

    }

    public void onClick(View view){
        switch(view.getId()){
            case R.id.musicBtn:
                Intent musicIntent=new Intent(MainActivity.this, MusicPlayer.class);
                startActivity(musicIntent);
                break;
            case R.id.videoBtn:
                Intent videoIntent=new Intent(MainActivity.this, VideoPlayer.class);
                startActivity(videoIntent);
                break;
            case R.id.twoBarCodeBtn:

                startQrCode();
//                Intent twoBarCodeIntent=new Intent(MainActivity.this, WiFi.class);
//                startActivity(twoBarCodeIntent);
                break;
            default:
                break;
        }
    }



    // 开始扫码
    private void startQrCode() {
        // 申请相机权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
            return;
        }
        // 申请文件读写权限（部分朋友遇到相册选图需要读写权限的情况，这里一并写一下）
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Constant.REQ_PERM_EXTERNAL_STORAGE);
            return;
        }
        // 二维码扫码
//        Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
        Intent intent = new Intent(MainActivity.this, Scan.class);
        startActivityForResult(intent, Constant.REQ_QR_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //扫描结果回调
        if (requestCode == Constant.REQ_QR_CODE && resultCode == RESULT_OK) {
            String scanResult = data.getStringExtra(Constant.INTENT_EXTRA_KEY_QR_SCAN);
            //拿到扫描结果 ，ssid  pw，连接wifi
            Log.d(MainActivity.class.getSimpleName(), "onActivityResult: " + scanResult);
            Intent intent = new Intent(MainActivity.this, WifiConnect.class);
            intent.putExtra("data",""+scanResult);
            startActivity(intent);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case Constant.REQ_PERM_CAMERA:
                // 摄像头权限申请
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 获得授权
                    startQrCode();
                } else {
                    // 被禁止授权
                    Toast.makeText(MainActivity.this, "请至权限中心打开本应用的相机访问权限", Toast.LENGTH_LONG).show();
                }
                break;
            case Constant.REQ_PERM_EXTERNAL_STORAGE:
                // 文件读写权限申请
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 获得授权
                    startQrCode();
                } else {
                    // 被禁止授权
                    Toast.makeText(MainActivity.this, "请至权限中心打开本应用的文件读写权限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

}
